﻿@{
    var nextText = HasText(Model.NextText) ? Model.NextText : T("Older").Text;
    var previousText = HasText(Model.PreviousText) ? Model.PreviousText : T("Newer").Text;

    var routeData = new RouteValueDictionary(ViewContext.RouteData.Values);
    var queryString = ViewContext.HttpContext.Request.QueryString;
    if (queryString != null) {
        foreach (string key in queryString.Keys) {
            if (key != null && !routeData.ContainsKey(key)) {
                var value = queryString[key];
                routeData[key] = queryString[key];
            }
        }
    }

    if (routeData.ContainsKey("id") && !HasText(routeData["id"])) {
        routeData.Remove("id");
    }

    // HACK: MVC 3 is adding a specific value in System.Web.Mvc.Html.ChildActionExtensions.ActionHelper
    // when a content item is set as home page, it is rendered by using Html.RenderAction, and the routeData is altered
    // This code removes this extra route value
    var removedKeys = routeData.Keys.Where(key => routeData[key] is DictionaryValueProvider<object>).ToList();
    foreach(string key in removedKeys) {
        routeData.Remove(key);
    }
        
    var hasNextPage = (Model.Page * Model.PageSize) < Model.TotalItemCount;

    Model.Classes.Add("pager");
    Model.Classes.Add("group");
    var tag = Tag(Model, "ul");
}
@if (hasNextPage || Model.Page > 1) {
    @tag.StartElement
    if(hasNextPage) {
        routeData["page"] = Model.Page + 1;
        <li class="page-next">
        @Html.ActionLink((string)nextText, (string)routeData["action"], (string)routeData["controller"], routeData, null)
        </li>
    }
    if(Model.Page > 1) {
        routeData["page"] = Model.Page - 1;
        <li class="page-previous">
        @Html.ActionLink((string)previousText, (string)routeData["action"], (string)routeData["controller"], routeData, null)
        </li>
    }
    @tag.EndElement
}